package com.oocl.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import util.JsonUtil;

import com.oocl.pojo.Customer;
import com.oocl.service.CustomerService;
import com.oocl.service.impl.CustomerServiceFactory;

@Controller
public class CustomerController {
	
	private static CustomerService customerService;
	
	public CustomerController() {
		customerService = CustomerServiceFactory.getCustomerManager();
		System.out.println("实例化controller");
	}
	
	@ResponseBody
	@RequestMapping(value="addCustomer",consumes="application/json")
	public String addCustomer(@RequestBody Customer customer){
		customerService.addCustomer(customer);
		
		String json = JsonUtil.toJson(customerService.findAll());
		return json;
	}
	
	@ResponseBody
	@RequestMapping(value="delCustomer")
	public String deleteCustomer(String id){
//		System.out.println(id);
		boolean flag = customerService.deleteCustomer(id);
		if(flag){
			return "delete successful";
		}else{
			return "delete fail";
		}

	}
	
	@ResponseBody
	@RequestMapping(value="findAll")
	public String findAll(){
		List<Customer> list = customerService.findAll();
		String json = JsonUtil.toJson(list);
		return json;
	}
}
